package www.bd.com.controller;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import www.bd.com.pojo.Label;
import www.bd.com.service.LabelService;

import java.util.List;

/**
 * @author Berret
 * @date 2019/4/11 15:44
 * @discription :
 */
@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return  new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
    }

    /**
     *
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findById(labelId));
    }

    /**
     *
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label){
        labelService.add(label);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     *
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("labelId") String labelId){
        labelService.deleteById(labelId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     *
     * @param labelId
     * @param label
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result update(@PathVariable("labelId") String labelId,@RequestBody Label label){
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     *
     * @param label
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.POST)
    public Result findsearch(@RequestBody Label label){
        List<Label> labelList = labelService.findSearch(label);
        return new Result(true,StatusCode.OK,"查询成功",labelList);
    }

    @RequestMapping(value = "search/{page}/{size}",method = RequestMethod.POST)
    public Result pageQuery(@PathVariable int page,@PathVariable int size, @RequestBody Label label){
        Page<Label> pageData = labelService.pageQuery(page,size,label);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Label>(pageData.getTotalElements(),pageData.getContent()));
    }
}
